using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace SuDokuSolution
{
	
	/// The graphicsl cell to be drawn on the screen, containing cell
	/// data and mouse events
	
    public class Cell_4_by_4 : GraphicalCell

	{
   

        
	
		/// Reference to the parent grid of the  cell
		
		private GridControl_4_by_4 parentGrid;

		
	
        public Cell_4_by_4()
        {
                
        }
		public Cell_4_by_4(GridControl_4_by_4 gc,Brush b)
		{
            one =new Constants();
			this.parentGrid=gc;
			this.backGroundBrush=b;
			this.CheckedNbs=new ArrayList(one.nbOfCells);
			this.cellData=new CellData();
		}



        /// Method to paint a cell
        public virtual void Paint(Graphics graphics)
        {
            //fill background
            graphics.FillRectangle(backGroundBrush, cellRect);

            if (!redraw)
                return;

            Brush selBrush;
            if (cellData.problemNb == one.NoNumber)
            {
                //draw vertical lines
                for (int i = 1; i < one.nbOfLines; i++)
                {
                    graphics.DrawLine(DrawingTools.LightPen,
                        cellLocation.X + subCellSize.Width * i, cellLocation.Y + 1,
                        cellLocation.X + subCellSize.Width * i, cellLocation.Y + cellSize.Height - 1);
                }

                //draw horizontal lines
                for (int i = 1; i < one.nbOfLines; i++)
                {
                    graphics.DrawLine(DrawingTools.LightPen,
                        cellLocation.X + 1, cellLocation.Y + subCellSize.Height * i,
                        cellLocation.X + cellSize.Width - 1, cellLocation.Y + subCellSize.Height * i);
                }

                //draw marked nbs
                foreach (Object ob in this.CheckedNbs)
                {
                    int nb = (int)ob;
                    if (this.cellData.candidateNb.Contains(nb) || !parentGrid.simultaneousCheck)            //if valid nb
                        selBrush = DrawingTools.AnswerBrush;
                    else
                        selBrush = DrawingTools.ErrorBrush;

                    SizeF fontSize = graphics.MeasureString(nb.ToString(), DrawingTools.miniSelectedFont);
                    graphics.DrawString(nb--.ToString(), DrawingTools.miniSelectedFont,
                        selBrush, cellLocation.X + subCellSize.Width * (nb % one.nbOfLines) + (subCellSize.Width - fontSize.Width) / 2,
                        cellLocation.Y + subCellSize.Height * (nb / one.nbOfLines) + (subCellSize.Height - fontSize.Height) / 2);
                }
            }

            else
            {

                if (cellData.readOnly)
                    selBrush = DrawingTools.ProblemBrush;
                else if (!MarkError || !parentGrid.simultaneousCheck)
                    selBrush = DrawingTools.AnswerBrush;
                else
                    selBrush = DrawingTools.ErrorBrush;

                SizeF fontSize = graphics.MeasureString(cellData.problemNb.ToString(), DrawingTools.SelectedFont);

                graphics.DrawString(cellData.problemNb.ToString(), DrawingTools.SelectedFont,
                    selBrush, cellLocation.X + (cellSize.Width - fontSize.Width) / 2,
                    cellLocation.Y + (cellSize.Height - fontSize.Height) / 2);
            }
        }

        /// processes the mouse single click event 
		public virtual void MouseDown(MouseEventArgs e)
		{	
			if(cellData.readOnly)
				return;
			
			int clickedNb=getClickedNb(e.X-cellLocation.X,e.Y-cellLocation.Y);
			if(cellData.problemNb==one.NoNumber)
			{
				if(CheckedNbs.Contains(clickedNb))
					CheckedNbs.Remove(clickedNb);
				else
					CheckedNbs.Add(clickedNb);
			}
			else{} //do nothing
			parentGrid.RedrawCell(this);
		}

        

	}
}
